package com.party.mobile.biz.copartner;

import com.google.common.base.Function;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.PartyCode;
import com.party.common.utils.StringUtils;
import com.party.core.exception.BusinessException;
import com.party.core.model.city.Area;
import com.party.core.model.copartner.CopartnerLevel;
import com.party.core.model.crowdfund.ProjectWithAuthor;
import com.party.core.model.distributor.DistributorCount;
import com.party.core.model.distributor.DistributorRelation;
import com.party.core.model.member.Industry;
import com.party.core.model.member.Member;
import com.party.core.model.system.MemberSysRole;
import com.party.core.model.system.RoleEnum;
import com.party.core.model.system.RoleType;
import com.party.core.model.system.SysRole;
import com.party.core.service.city.IAreaService;
import com.party.core.service.copartner.ICopartnerLevelService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.distributor.IDistributorCountService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.core.service.member.IIndustryService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.system.IMemberSysRoleService;
import com.party.core.service.system.ISysRoleService;
import com.party.mobile.biz.member.MemberBizService;
import com.party.mobile.web.dto.copartner.input.CopartnerRegisterInput;
import com.party.mobile.web.dto.copartner.output.GetDateOutput;
import com.party.mobile.web.dto.copartner.output.ProjectCountOutput;
import com.party.mobile.web.dto.copartner.output.RepresentProjectListOutput;
import com.party.mobile.web.dto.login.output.MemberOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Nullable;
import java.util.List;

/**
 * 联盟机构合作商业务接口
 * Created by wei.li.
 *
 * @date 2018/2/6 0006
 * @time 15:57
 */
@Service
public class CopartnerBizService {

    @Autowired
    private IDistributorRelationService distributorRelationService;

    @Autowired
    private IDistributorCountService distributorCountService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IMemberService memberService;

    @Autowired
    private IAreaService areaService;

    @Autowired
    private IIndustryService industryService;

    @Autowired
    private MemberBizService memberBizService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IMemberSysRoleService memberSysRoleService;
    @Autowired
    private ICopartnerLevelService copartnerLevelService;


    /**
     * 获取统计数据
     *
     * @param id 联合发起编号
     * @return 输出视图
     */
    public GetDateOutput getDate(String id){
        GetDateOutput getDateOutput = new GetDateOutput();
        DistributorRelation distributorRelation = distributorRelationService.get(id);
        if (null == distributorRelation){
            throw new BusinessException("联合发起数据不存在");
        }

        DistributorCount distributorCount
                = distributorCountService.findByRelationId(distributorRelation.getId());
        if (null != distributorCount){
            getDateOutput.setApplyNum(distributorCount.getCrowdfundNum());
            getDateOutput.setAmount(distributorCount.getFavorerAmount());
            getDateOutput.setSuccessNum(distributorCount.getSuccessNum());
            getDateOutput.setOrderNum(distributorCount.getFavorerNum());
        }
        getDateOutput.setCreateDate(distributorRelation.getCreateDate());
        getDateOutput.setEndTime(distributorRelation.getTargetEndDate());
        return getDateOutput;
    }


    /**
     * 联合发起下的众筹列表
     * @param isSuccess 是否成功
     * @param representId 联合发起编号
     * @param page 分页参数
     * @return 输出视图
     */
    public List<RepresentProjectListOutput> projectForRepresentList(Integer isSuccess, String representId, Page page){
        ProjectWithAuthor projectWithAuthor = new ProjectWithAuthor();
        projectWithAuthor.setIsSuccess(isSuccess);
        projectWithAuthor.setRelationId(representId);
        List<ProjectWithAuthor> projectWithAuthorList = projectService.listPage(projectWithAuthor, page);
        return LangUtils.transform(projectWithAuthorList, new Function<ProjectWithAuthor, RepresentProjectListOutput>() {
            @Nullable
            @Override
            public RepresentProjectListOutput apply(@Nullable ProjectWithAuthor projectWithAuthor) {
                return RepresentProjectListOutput.transform(projectWithAuthor);
            }
        });
    }


    /**
     * 联合发起众筹统计数
     *
     * @param representId 联合发起编号
     * @return 统计数
     */
    public ProjectCountOutput projectCount(String representId){
        ProjectCountOutput projectCountOutput = new ProjectCountOutput();
        DistributorCount distributorCount = distributorCountService.findByRelationId(representId);
        if (null != distributorCount){
            projectCountOutput.setSuccessNum(distributorCount.getSuccessNum());
            projectCountOutput.setApplyNum(distributorCount.getCrowdfundNum());
        }
        return projectCountOutput;
    }


    /**
     * 更新会员信息
     *
     * @param input 输入参数
     * @param memberId 会员编号
     * @return 交互数据
     */
    public MemberOutput updateMember(CopartnerRegisterInput input, String memberId){

        Member member = memberService.get(memberId);
        if (null == member){
            throw new BusinessException(PartyCode.CURRENT_USER_DATA_ERROR, "当前用户数据错误,请重新登录");
        }

        Area area = areaService.get(input.getCity());
        if (null == area){
            throw new BusinessException(PartyCode.AREA_DATA_ERROR, "当前输入的地区id有误，没有找到相应的地区信息");
        }

        Industry industry = industryService.get(input.getIndustry());
        if (null == industry) {
            throw new BusinessException(PartyCode.INDUSTRY_DATA_ERROR, "当前输入的行业id有误，没有找到相应的行业信息");
        }

        //更新会员数据
        member.setCompany(input.getCompany());
        member.setCity(input.getCity());
        member.setIndustry(input.getIndustry());
        member.setWantRes(input.getWantRes());
        member.setLogo(input.getLogo());
        member.setRealname(input.getCompany());
        if (StringUtils.isNotEmpty(input.getCountryCode())) {
            member.setCountryCode(input.getCountryCode());
        }
        memberService.update(member);

        MemberOutput output = memberBizService.getLoginMember(member);
        return output;
    }
}
